﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InnerFactory;
using MINERP.InnerFactory.IView;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;
using FastReport;
using System.Data;
using System.Xml;

namespace MINERP.InnerFactory.Presenter
{
    public class MainPresenter
    {
        public MINERP.BLL.InnerFactory.InnerFactoryBLL _bll;
        public IView.IFormMain _IView;
        private Speech _speech = null;
        public MainPresenter(IFormMain inIForm)
        {
            _IView = inIForm;
            _bll = new InnerFactoryBLL();
            //_IView.FactoryId = 2;//测试用
            _speech = new Speech();
        }
        public void initForm()
        {
            _IView.DisplayList(_bll.NewGetProduceViewList(_IView.FactoryId));
        }
        /// <summary>
        /// 
        /// </summary>
        public void StartProduce()
        {
            OutProcessRecord_Info info = null;
            if (Properties.Settings.Default.ScanType == 0)
            {
                info = _bll.GetReCordByCode(_IView.CurrBarCode);
            }
            else
            {
                info = _bll.GetReCordByPhotoCode(_IView.CurrBarCode);
            }
            if (info == null)
            {
                string strInfo="请确认" + _IView.CurrBarCode + "存在";
                if (Properties.Settings.Default.ScanVoice)
                {
                    MessageBox.Show(strInfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    _speech.Speak("产品不存在");
                }
                return;
            }
            string strinGridInfo="产品" + _IView.CurrBarCode + "在格子中未打印";
           
                if (info.Status == (int)OutProcessRecordStatus.InGrid)
                {
                    if (Properties.Settings.Default.ScanVoice)
                    {
                        _speech.Speak("在格子中未打印");
                    }
                    else
                    {
                        MessageBox.Show(strinGridInfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        
                    }
                    return;
                }
            //}
            //PrintBarCode();
            if (info.Status == (int)OutProcessRecordStatus.OutWaitFenDui)
            {
                string strunInGridInfo="产品" + _IView.CurrBarCode + "未分配格子";
                if (Properties.Settings.Default.ScanVoice)
                {
                    _speech.Speak(strunInGridInfo);
                }
                else
                {
                    MessageBox.Show(strunInGridInfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return;
            }

            //if (info.Status != (int)OutProcessRecordStatus.Outing)
            //{
            //    MessageBox.Show("产品"+_IView.CurrBarCode+"已完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            else
            {
                if (info.Status2 == (int)OutProcessRecordStatus2.Wait)
                {
                    //打出条码
                    Report report = new Report();
                    bool isBarcode = Properties.Settings.Default.IsBarCode;
                    if (isBarcode)
                    {
                        int Num = Properties.Settings.Default.intBarCode;

                        //创建XmlData
                        DataTable barDT = CreateDT();

                        for (int i = 0; i < Num; i++)
                        {
                            barDT.Rows.Add(_IView.CurrBarCode);
                        }
                        barDT.TableName = "barDT";
                        XmlDocument doc = new XmlDocument();
                        barDT.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
                        //report.RegisterData(barDT, "barDT");
                        report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
                        report.Show();
                        report.Print();


                    }
                   // PrintBarCode();
                   
                    if (isBarcode)
                    {
                        PrintBarCode();
                    }
                   // PrintBarCode();
                    //开始生产
                    _bll.StartProduce(_IView.CurrUserId,info.OutProcessRecordId);
                    ProduceView pv = _IView.listView.FirstOrDefault(p => p.ProductCode == _IView.CurrBarCode);
                    if (pv != null)
                    {
                        pv.ProduceInfo.StartProductTime = DateTime.Now;
                        pv.ProduceInfo.Status2 = (int)OutProcessRecordStatus2.Doing;
                    }
                    else
                    {
                        pv = _bll.GetProduceView(info.OutProcessRecordId);
                    }
                    _IView.RushOneRecord(pv);
                    //_IView.DisplayList(_bll.GetProduceViewList(_IView.FactoryId));
                    string strActive="产品" + _IView.CurrBarCode + "激活成功！";
                    if (Properties.Settings.Default.ScanVoice)
                    {
                        _speech.Speak("激活成功");
                    }
                    else
                    {
                        MessageBox.Show(strActive, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 
                    }
                    //PrintBarCode();
                    //initForm();
                    return;
                }
                else if (info.Status2 == (int)OutProcessRecordStatus2.Doing)
                {
                    //结束生产
                    if (MessageBox.Show("确认" + _IView.CurrBarCode + "生产完成?确认请按'确定'，否则请点击'取消'", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        _bll.EndProduce(_IView.CurrUserId, info.OutProcessRecordId);
                        string strActiveSuccess="产品" + _IView.CurrBarCode + "结束生产，请将标签贴到产品上！";
                        if (Properties.Settings.Default.ScanVoice)
                        {
                            _speech.Speak("完成");
                        }
                        else
                        {
                            MessageBox.Show(strActiveSuccess, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        PrintBarCode();
                        //initForm();
                        ProduceView pv = _IView.listView.FirstOrDefault(p => p.ProductCode == _IView.CurrBarCode);
                        
                        if (pv != null)
                        {
                            pv.ProduceInfo.StartProductTime = DateTime.Now;
                            pv.ProduceInfo.Status2 = (int)OutProcessRecordStatus2.Complete;
                        }
                        else
                        {
                            pv = _bll.GetProduceView(info.OutProcessRecordId);
                        }
                        _IView.RushOneRecord(pv);
                        return;
                    }
                }
                else if (info.Status2 == (int)OutProcessRecordStatus2.Error)
                {
                    //恢复生产
                    if (MessageBox.Show("确认继续生产该返工产品（" + _IView.CurrBarCode + "）?确认请点击'确定'，否则请点击'取消'", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        _bll.RebootProduce(_IView.CurrUserId, info.OutProcessRecordId);
                        string strResume="产品" + _IView.CurrBarCode + "恢复生产！";
                        if(Properties.Settings.Default.ScanVoice)
                        {
                            _speech.Speak("恢复成功");
                        }else
                        {
                        MessageBox.Show(strResume, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        ProduceView pv = _IView.listView.FirstOrDefault(p => p.ProductCode == _IView.CurrBarCode);
                        if (pv != null)
                        {
                            pv.ProduceInfo.StartProductTime = DateTime.Now;
                            pv.ProduceInfo.Status2 = (int)OutProcessRecordStatus2.Doing;
                        }
                        else
                        {
                            pv = _bll.GetProduceView(info.OutProcessRecordId);
                        }
                        _IView.RushOneRecord(pv);
                        //initForm();
                        return;
                    }
                }
                else
                {
                    if (DialogResult.OK == MessageBox.Show("产品" + _IView.CurrBarCode + "已生产完成！是否重新打印不干胶标签？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
                    {
                        PrintBarCode();
                    }
                    else
                    {

                        return;
                    }
                }
            }
        }
        void RushOneRecord(int OutProcessRecordId)
        {
            //ProduceView produceView=_bll.GetProduceView(OutProcessRecordId);
            //_IView.RushOneRecord(produceView);
        }
        public void PrintBarCode()
        {
            ////打印当前的条码
            //Report report = new Report();
            //bool isBarcode = Properties.Settings.Default.IsBarCode;
            //if (isBarcode)
            //{
            //    int Num = Properties.Settings.Default.intBarCode;

            //    //创建XmlData
            //    DataTable barDT = CreateDT();

            //    for (int i = 0; i < Num; i++)
            //    {
            //        barDT.Rows.Add(_IView.CurrBarCode);
            //    }
            //    barDT.TableName = "barDT";
            //    XmlDocument doc = new XmlDocument();
            //    barDT.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
            //    //report.RegisterData(barDT, "barDT");
            //    report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
            //    //report.Show();
            //    report.Print();
            //}
            Report report = new Report();
            DataTable table = _bll.GetPrintInfo(_IView.CurrBarCode);
            XmlDocument doc = new XmlDocument();
            table.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
            report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
            report.PrintSettings.ShowDialog = false;
            //report.Show();
            report.Print();

        }

        public void PrintBarCode(string barcode)
        {

            Report report = new Report();
            DataTable table = _bll.GetPrintInfo(barcode);
            XmlDocument doc = new XmlDocument();
            table.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
            report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
            report.PrintSettings.ShowDialog = false;
            //report.Show();
            report.Print();

        }


        /// <summary>
        /// 创建DT
        /// </summary>
        /// <returns></returns>
        public DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BarCode", typeof(string));
            return dt;
        }

    }
}
